package com.example.tys.baseapplication.NetModel.okhttp;

import android.content.Context;
import android.content.res.AssetManager;
import android.support.annotation.NonNull;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import okhttp3.Call;
import okhttp3.OkHttpClient;


/**
 * @author Administrator
 * @date 2016/10/26
 * 设置OkHttp的基础参数设置和任务取消类
 */
public class OkHttpClientManger {
    private static final String TAG = "OkHttpClientManger";
    private static long DEFAULT_CONNECT_TIMEOUT = 15_000;
    private static long DEFAULT_READ_TIMEOUT = 8_000;
    private static long DEFAULT_WRITE_TIMEOUT = 8_000;
    private static Hashtable<String, OkHttpClient> CLIENT_HASH_TABLE = new Hashtable<>(4);
    private static OkHttpClientManger mInstance;
    private static OkHttpsSslUtils.SslParams sslParams;

    private OkHttpClientManger() {
    }

    public static OkHttpClientManger getInstance() {
        if (mInstance == null) {
            synchronized (OkHttpClientManger.class) {
                if (mInstance == null) {
                    mInstance = new OkHttpClientManger();
                }
            }
        }
        return mInstance;
    }

    public static void init(Context context) {
//        try {
//            sslParams = OkHttpsSslUtils.createdSslParams(
//                    new InputStream[]{
//                            context.getAssets().open("tomcat.cer", AssetManager.ACCESS_RANDOM),
//                            context.getAssets().open("baidu.cer", AssetManager.ACCESS_RANDOM),
//                    },
//                    null, null);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    private static OkHttpClient createdClient(String tag) {
        return getDefaultClient(tag);
    }

    private static OkHttpClient getDefaultClient(String tag) {
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .connectTimeout(DEFAULT_CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)
                .readTimeout(DEFAULT_READ_TIMEOUT, TimeUnit.MILLISECONDS)
                .writeTimeout(DEFAULT_WRITE_TIMEOUT, TimeUnit.MICROSECONDS)
                .hostnameVerifier(new MyHostnameVerifier());
        if (sslParams != null) {
            builder.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager);
        }
        return builder.build();
    }

    private static class MyHostnameVerifier implements HostnameVerifier {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            Log.i(TAG, hostname);
            return true;
        }
    }

    public void cancel(@NonNull OkHttpClient client, Object tag) {
        List<Call> waitingCall = client.dispatcher().queuedCalls();
        for (Call call : waitingCall) {
            if (tag.equals(call.request().tag())) {
                call.cancel();
            }
        }

        List<Call> runningCall = client.dispatcher().runningCalls();
        for (Call call : runningCall) {
            if (tag.equals(call.request().tag())) {
                call.cancel();
            }
        }
    }


    public static OkHttpClient getClient(String tag) {
        OkHttpClient client = CLIENT_HASH_TABLE.get(tag);
        if (null == client) {
            client = createdClient(tag);
            setClient(tag, client);
        }
        return client;
    }

    private static void setClient(String tag, OkHttpClient client) {
        CLIENT_HASH_TABLE.put(tag, client);
    }
}
